import axios from 'axios'
import { ElMessageBox, ElMessage } from 'element-plus'
import { showFullScreenLoading, tryHideFullScreenLoading } from '@/config/serviceLoading'
import { getToken } from '@/utils/cookies'
// import { useMicro } from '@/hooks/useMicro'
import { useUserStore } from '@/store/modules/user'
export default config => {
  const service = axios.create({
    baseURL: import.meta.env.VITE_APP_ENV === 'production' ? import.meta.env.VITE_APP_BASE_API : '/api',
    timeout: 50000
  })
  // 请求拦截器
  service.interceptors.request.use(
    config => {
      // 当前请求不需要显示 loading，在 api 服务中通过指定的第三个参数: { noLoading: true } 来控制
      config.noLoading || showFullScreenLoading()
      config.headers['Authorization'] = getToken() || ''
      config.headers['Content-Type'] = config.headers['Content-Type'] || 'application/json'
      // 8080
      if (config.type == 'file') {
        config.headers['content-type'] = 'application/multipart/form-data'
      } else if (config.type == 'form') {
        config.headers['Content-type'] = 'application/x-www-form-urlencoded'
      }
      if (config.method && config.method.toLowerCase() === 'get') {
        config.data = true
      }
      return config
    },
    error => {
      return Promise.reject(error)
    }
  )
  // 响应拦截器
  service.interceptors.response.use(
    response => {
      const code = response.data.code
      // const micro = useMicro()
      tryHideFullScreenLoading()
      if (code === 401) {
        ElMessageBox.confirm('登录状态已过期，您可以继续留在该页面，或者重新登录', '系统提示', {
          confirmButtonText: '重新登录',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          // 调用退出登录接口
          await useUserStore().LogOut()
          // window.location.href = micro.isMicro ? window.localStorage.getItem('mainJumpPublicPath') || '/' : window.location.origin
        })
      } else if (code !== 200) {
        ElMessage({
          message: response.data.msg,
          type: 'error',
          duration: 5 * 1000
        })
        return Promise.reject('error')
      } else {
        return response.data
      }
    },
    error => {
      tryHideFullScreenLoading()
      ElMessage({
        message: error.message,
        type: 'error',
        duration: 5 * 1000
      })
      return Promise.reject(error)
    }
  )
  return service(config)
}
